package com.eonmind.pay.mapper;

import com.eonmind.common.contract.data.base.BaseMongoMapper;
import com.eonmind.common.contract.enums.DelEnum;
import com.eonmind.common.provider.entity.pay.Cart;
import com.eonmind.pay.config.WxPayConfig;
import com.eonmind.pay.enums.TradeState;
import com.mongoplus.conditions.query.QueryWrapper;
import com.mongoplus.conditions.update.UpdateWrapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @ClassName CartMapper
 * @Description TODO
 * Author 32004
 * Date 2025/5/27 14:13
 * Version 1.0
 **/

@Component
public class CartMapper extends BaseMongoMapper<Cart> {



    public List<Cart> selectList(Long id) {
        QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId",id);
        queryWrapper.eq("del", DelEnum.UN_DEL.getCode());
        queryWrapper.exists("orderId",false);
        queryWrapper.eq("status", TradeState.ADD.getCode());
        return this.list(queryWrapper);
    }

    public void removeCart(Long userId, Long productId) {
        UpdateWrapper<Cart> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("userId", userId);
        updateWrapper.eq("productId", productId);
        updateWrapper.exists("orderId",false);
        updateWrapper.eq("del", DelEnum.UN_DEL.getCode());
        this.remove(updateWrapper);
    }

    public void updateCartCount(Long userId, Long productId, Integer count) {
        UpdateWrapper<Cart> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("userId", userId);
        updateWrapper.eq("productId", productId);
        updateWrapper.exists("orderId",false);
        updateWrapper.eq("del", DelEnum.UN_DEL.getCode());
        updateWrapper.set("count", count);
        this.update(updateWrapper);
    }

    public List<Cart> selectListByOrderId(Long id) {
        QueryWrapper<Cart> wrapper = new QueryWrapper<>();
        wrapper.eq(Cart::getOrderId, id);
        return this.list(wrapper);
    }



    public void updateOrderId(Long id,Long orderId) {
        UpdateWrapper<Cart> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq(Cart::getId, id);
        updateWrapper.set(Cart::getOrderId, orderId);
        System.out.println(this.update(updateWrapper));
    }

    public List<Cart> selectCartByProductId(Long productId, Long userId) {
        QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("productId", productId);
        queryWrapper.eq("userId", userId);
        return this.list(queryWrapper);
    }
}
